<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/6
 * Time: 下午4:22
 */
class Login_hooks
{
    private $CI;
    private $not_in_login, $not_in_activation;

    public function __construct()
    {
        $this->CI = &get_instance();
        $this->CI->load->database();
        $this->CI->load->library('form_validation');//加载表单验证类库
        $this->CI->load->helper('json');
        $this->CI->load->helper('url');
        $this->CI->load->helper('common');
        $this->CI->load->library('session');
        $this->not_in_login = array(
            'approve' => array(),
            'bill' => array('bill_index'),
            'cart' => array(),
            'goods' => array(),
            'member' => array(),
            'manager_user' => array('insure_activation'),
            'customer' => array(),
            'news' => array(),
            'office' => array(),
            'order' => array(),
            'index' => array('index', 'index_desk', 'index_activation', 'index_freeze'),
            'report' => array(),
            'information' => array(),
            'multiple_address' => array(),
            'cost_center' => array('cost_center_index', 'cost_center_user_present', 'cost_center_present', 'cost_center_manage_present', 'cost_center_address_present', 'no_permission_index')
        );
        $this->not_in_activation = array(
            'approve' => array(),
            'bill' => array(),
            'cart' => array(),
            'goods' => array(),
            'member' => array(),
            'manager_user' => array('insure_activation'),
            'customer' => array(),
            'news' => array(),
            'office' => array(),
            'order' => array(),
            'report' => array(),
            'information' => array(),
            'multiple_address' => array(),
            'index' => array('index_activation', 'index_freeze'),
            'cost_center' => array()
        );
    }

    public function pre_login()
    {
        $class = $this->CI->router->class;
        $method = $this->CI->router->method;
        if (isset($this->not_in_login[$class]) && !in_array($method, $this->not_in_login[$class])) {
            $customer_id = $this->CI->session->customer_id;
            if ($customer_id) {
                $sql = 'select * from app_customer WHERE id = ' . $customer_id;
                $res = $this->CI->db->query($sql)->row_array();
                //如果不是主账号
                if ($res['identity'] != 1) {
                    $sql = 'select activation from app_manager_user WHERE customer_id = ' . $customer_id;
                    $res = $this->CI->db->query($sql)->row_array();
                    if ($res['activation'] != 1) {
                        // 没有激活，返回激活页面
                        redirect('index/index_activation');
                        exit;
                    }
                }

            } else {
                // 没有登录返回首页
                header('Location: http://www.dev.wjyh.com/mMLogin.html');
                exit;
            }
        }

        if (isset($this->not_in_activation[$class]) && !in_array($method, $this->not_in_activation[$class])) {
            $customer_id = $this->CI->session->customer_id;
            if ($customer_id) {
                $sql = 'select * from app_customer WHERE id = ' . $customer_id;
                $res = $this->CI->db->query($sql)->row_array();
                if ($res['status'] == 0) {
                    redirect('index/index_freeze');
                    exit;
                }

                //如果不是主账号
                if ($res['identity'] != 1) {
                    $sql = 'select activation from app_manager_user WHERE customer_id = ' . $customer_id;
                    $res = $this->CI->db->query($sql)->row_array();
                    if ($res['activation'] != 1) {
                        // 没有激活，返回激活页面
                        redirect('index/index_activation');
                        exit;
                    }
                }
            }
        }


    }


}